﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace TeenyGrab.Helpers
{
    public static class Extensions
    {
        public static Stream ToStream(this Image image, ImageFormat formaw)
        {
            var stream = new System.IO.MemoryStream();
            image.Save(stream, formaw);
            stream.Position = 0;
            return stream;
        }

        public static System.Windows.Input.Key WinformsToWPFKey(this System.Windows.Forms.Keys inputKey)
        {
            // Put special case logic here if there's a key you need but doesn't map...  
            try
            {
                return (System.Windows.Input.Key)Enum.Parse(typeof(System.Windows.Input.Key), inputKey.ToString());
            }
            catch
            {
                // There wasn't a direct mapping...    
                return System.Windows.Input.Key.None;
            }
        }
        
        public static System.Windows.Forms.Keys WPFToWinformsKey(this System.Windows.Input.Key inputKey)
        {
            // Put special case logic here if there's a key you need but doesn't map...  
            try
            {
                return (System.Windows.Forms.Keys)Enum.Parse(typeof(System.Windows.Forms.Keys), inputKey.ToString());
            }
            catch
            {
                // There wasn't a direct mapping...    
                return System.Windows.Forms.Keys.None;
            }
        } 

        public static BitmapImage ToBitmapImage(this Bitmap input)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                input.Save(memory, ImageFormat.Png);
                memory.Position = 0;
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = memory;
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.EndInit();
                return bitmap;
            }
        }

        public static Image Crop(this Image image, Area area)
        {
            Bitmap bmpImage = new Bitmap(image);
            Bitmap bmpCrop = bmpImage.Clone(new Rectangle(area.X, area.Y, area.X2 - area.X, area.Y2 - area.Y), bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }

        public static string GetExtension(this ImageFormat imageFormat)
        {
            if (imageFormat == ImageFormat.Bmp)
                return "bmp";
            else if (imageFormat == ImageFormat.Emf)
                return "emf";
            else if (imageFormat == ImageFormat.Exif)
                return "exif";
            else if (imageFormat == ImageFormat.Gif)
                return "gif";
            else if (imageFormat == ImageFormat.Icon)
                return "ico";
            else if (imageFormat == ImageFormat.Jpeg)
                return "jpg";
            else if (imageFormat == ImageFormat.Png)
                return "png";
            else if (imageFormat == ImageFormat.Tiff)
                return "tiff";
            else if (imageFormat == ImageFormat.Wmf)
                return "wmf";

            return "";
        }

        public static ImageFormat ToImageFormat(this string str)
        {
            var strLower = str.ToLower();
            if (strLower == "bmp")
                return ImageFormat.Bmp;
            else if (strLower == "emf")
                return ImageFormat.Emf;
            else if (strLower == "exif")
                return ImageFormat.Exif;
            else if (strLower == "gif")
                return ImageFormat.Gif;
            else if (strLower == "ico")
                return ImageFormat.Icon;
            else if (strLower == "jpg" || strLower == "jpeg")
                return ImageFormat.Jpeg;
            else if (strLower == "png")
                return ImageFormat.Png;
            else if (strLower == "tiff")
                return ImageFormat.Tiff;
            else if (strLower == "wmf")
                return ImageFormat.Wmf;

            return null;
        }
    }
}
